package com.ybg.engine.task;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ybg.engine.config.ScheduleTimeHelper;
import com.ybg.engine.report.sign.SignReportService;
import com.ybg.engine.report.sign.SignReportUniversityPo;
import com.ybg.util.DateUtil;
import com.ybg.util.LogUtil;

public class SignReportInitTask extends Task {

	private static boolean running = false;

	@Override
	public void run() throws Exception {
		if (!enter()) {
			throw new RuntimeException("调度已经在进行中");
		}
		LogUtil.info("SignReportInitTask start ");

		ScheduleTimeHelper scheduleTimeHelper = new ScheduleTimeHelper(super.jdbcTemplate);
		SignReportService service = new SignReportService(super.jdbcTemplate);

		// 初始化 5月1日 到 5月24日数据
		Calendar cale = Calendar.getInstance();
		cale.setTime(DateUtil.SIMPLE_FORMAT.parse("2017-05-01"));
		for (int i = 0; i < 24; ++i) {
			Date day = cale.getTime();
			// 当前周
			int cycle = scheduleTimeHelper.getCycle(day);
			// 日报
			savePo(service.getDayReport(DateUtil.formatSimple(day)));
			// 周报
			savePo(service.getWeekReport(String.valueOf(cycle)));
			// 月报
			savePo(service.getMonthReport(DateUtil.format(day, "yyyy-MM")));
			// next day
			cale.add(Calendar.DAY_OF_MONTH, 1);
		}

		leaving();
		LogUtil.info("SignReportInitTask end ");
	}

	@Override
	public String setSuccessResultMsg() {
		return "点名统计初始化调度成功";
	}

	private synchronized boolean enter() {
		if (running) {
			return false;
		} else {
			running = true;
			return true;
		}
	}

	private synchronized void leaving() {
		running = false;
	}

	private void savePo(SignReportUniversityPo po) {
		if (isExists(po)) {
			updatePo(po);
		} else {
			insertPo(po);
		}
	}

	private void insertPo(SignReportUniversityPo po) {
		String sql = "insert into YKT_BR_SIGN_UNIVERSITY(report_id,university_code,report_cycle,report_time,"
				+ "school_year,school_term,attendance_rate,absence_num,usage_course_num,course_num,"
				+ "usage_teacher_num,usage_student_num,update_time) values(sys_guid(),?,?,?,?,?,?,?,?,?,?,?,?)";
		List<Object> args = new ArrayList<Object>(12);
		args.add(po.getUniversityCode());
		args.add(po.getReportCycle().ordinal());
		args.add(po.getReportTime());
		args.add(po.getSchoolYear());
		args.add(po.getSchoolTerm());
		args.add(po.getAttendanceRate());
		args.add(po.getAbsenceNum());
		args.add(po.getUsageCourseNum());
		args.add(po.getCourseNum());
		args.add(po.getUsageTeacherNum());
		args.add(po.getUsageStudentNum());
		args.add(new Date());
		super.jdbcTemplate.update(sql, args.toArray());
	}

	private void updatePo(SignReportUniversityPo po) {
		String sql = "update YKT_BR_SIGN_UNIVERSITY set attendance_rate =? ,absence_num=?,usage_course_num=?"
				+ ",course_num=?,usage_teacher_num=?,usage_student_num=?,update_time=? where "
				+ "university_code = ? and report_cycle = ? and report_time = ? and school_year = ? and school_term = ?";
		List<Object> args = new ArrayList<Object>(12);
		args.add(po.getAttendanceRate());
		args.add(po.getAbsenceNum());
		args.add(po.getUsageCourseNum());
		args.add(po.getCourseNum());
		args.add(po.getUsageTeacherNum());
		args.add(po.getUsageStudentNum());
		args.add(new Date());

		args.add(po.getUniversityCode());
		args.add(po.getReportCycle().ordinal());
		args.add(po.getReportTime());
		args.add(po.getSchoolYear());
		args.add(po.getSchoolTerm());

		super.jdbcTemplate.update(sql, args.toArray());
	}

	private boolean isExists(SignReportUniversityPo po) {
		String sql = "select count(*) from YKT_BR_SIGN_UNIVERSITY where university_code = ? and "
				+ "report_cycle = ? and report_time = ? and school_year = ? and school_term = ?";
		List<Object> args = new ArrayList<Object>(12);
		args.add(po.getUniversityCode());
		args.add(po.getReportCycle().ordinal());
		args.add(po.getReportTime());
		args.add(po.getSchoolYear());
		args.add(po.getSchoolTerm());

		return super.jdbcTemplate.queryForObject(sql, Number.class, args.toArray()).intValue() > 0;
	}

}
